翻訳と辞書
Words near each other
・ Genesee County Sheriff's Office (New York)
・ Genesee County, Michigan
・ Genesee County, New York
・ Genesee Cream Ale
・ Genesee Democrat
・ Genesee Depot, Wisconsin
・ Genesee Exchange Bank
・ Genesee Falls, New York
・ Genesee Generating Station
・ Genesee Group
・ Genesee Hospital
・ Genesee Intermediate School District
・ Generator (Bad Religion album)
・ Generator (category theory)
・ Generator (circuit theory)
Generator (computer programming)
・ Generator (Foo Fighters song)
・ Generator (mathematics)
・ Generator (The Holloways song)
・ Generator function
・ Generator Gawl
・ Generator interlock kit
・ Generator matrix
・ Generator Rex
・ Generator Sound Art
・ Generator Tour
・ Generator X (band)
・ Generator zla
・ Generatrix
・ Generał


Dictionary Lists
翻訳と辞書 辞書検索 [ 開発暫定版 ]
スポンサード リンク

Generator (computer programming) : ウィキペディア英語版
Generator (computer programming)

In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. In fact, all generators are iterators.〔http://stackoverflow.com/questions/1022564/what-is-the-difference-between-an-iterator-and-a-generator〕 A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values. However, instead of building an array containing all the values and returning them all at once, a generator yields the values one at a time, which requires less memory and allows the caller to get started processing the first few values immediately. In short, a generator ''looks like'' a function but ''behaves like'' an iterator.
Generators can be implemented in terms of more expressive control flow constructs, such as coroutines or first-class continuations. Generators, also known as semicoroutines, are a special case of (and weaker than) coroutines, in that they always yield control back to the caller (when passing a value back), rather than specifying a coroutine to jump to; see comparison of coroutines with generators.
==Uses==

Generators are usually invoked inside loops.〔The Icon Programming Language utilizes generators to implement its goal directed evaluation. In Icon, generators can be invoked in contexts outside of the normal looping control structures.〕 The first time that a generator invocation is reached in a loop, an iterator object is created that encapsulates the state of the generator routine at its beginning, with arguments bound to the corresponding parameters. The generator's body is then executed in the context of that iterator until a special ''yield'' action is encountered; at that time, the value provided with the ''yield'' action is used as the value of the invocation expression. The next time the same generator invocation is reached in a subsequent iteration, the execution of the generator's body is resumed after the ''yield'' action, until yet another ''yield'' action is encountered. In addition to the ''yield'' action, execution of the generator body can also be terminated by a ''finish'' action, at which time the innermost loop enclosing the generator invocation is terminated. In more complicated situations, a generator may be used manually outside of a loop to create an iterator, which can then be used in various ways.
Because generators compute their yielded values only on demand, they are useful for representing streams, such as sequences that would be expensive or impossible to compute at once. These include e.g. infinite sequences and live data streams.
When eager evaluation is desirable (primarily when the sequence is finite, as otherwise evaluation will never terminate), one can either convert to a list, or use a parallel construction that creates a list instead of a generator. For example in Python a generator g can be evaluated to a list l via l = list(g), while in F# the sequence expression seq evaluates lazily (a generator or sequence) but (... ) evaluates eagerly (a list).
In the presence of generators, loop constructs of a language – such as for and while – can be reduced into a single loop ... end loop construct; all the usual loop constructs can then be comfortably simulated by using suitable generators in the right way. For example, a ranged loop like for x = 1 to 10 can be implemented as iteration through a generator, as in Python's for x in xrange(10, 1). Further, break can be implemented as sending ''finish'' to the generator and then using continue in the loop.

抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)
ウィキペディアで「Generator (computer programming)」の詳細全文を読む



スポンサード リンク
翻訳と辞書 : 翻訳のためのインターネットリソース

Copyright(C) kotoba.ne.jp 1997-2016. All Rights Reserved.